package com.adcc.foc.service;

import com.adcc.foc.IDao.OrgMapper;
import com.adcc.foc.domain.Organization;
import com.adcc.foc.domain.TreeNode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by houxy on 2016/8/3.
 */
@Service("organizationService")
@Transactional(rollbackFor = Exception.class)
public class OrganizationServiceImpl implements OrganizationService {
    @Resource
    private OrgMapper orgMapper;

    /**
     * 查找当前部门的根节点
     *
     * @param orgId
     * @return
     */
    public String findRoot(String orgId) {
        //反推回当前部门的所有上级部门，所以根节点为list最后一个部门
        List<Organization> list = orgMapper.findRoot(orgId);
        return list.get(list.size() - 1).getId();
    }

    /**
     * 获取所有节点信息
     *
     * @author houxy
     */
    @Override
    public List<TreeNode> getNodeList(String orgId, String isShowAll) {
        TreeNode treeNode = new TreeNode();
        List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
        List<Organization> list = new ArrayList<Organization>();
        if ("0".equals(isShowAll) || "1".equals(isShowAll)) {
            list = orgMapper.findAll();
        } else if ("2".equals(isShowAll)) {
            //获取可以维护的部门
            list = orgMapper.findOrgsByOrgId(findRoot(orgId));
        }
        for (Organization org : list) {
            treeNode = new TreeNode(org.getId(), org.getPid(), org.getDeptname(), "true");
            treeNodeList.add(treeNode);
        }
        return treeNodeList;
    }

    /**
     * 修改节点名称
     *
     * @param id       选中节点的id
     * @param deptName 修改后的名字
     * @author houxy
     */
    @Override
    public Map<String, Object> alterName(String id, String deptName) {
        Organization org = new Organization();
        Map<String, Object> map = new HashMap<String, Object>();
        Organization oldOrg = orgMapper.findByOrgId(id);
        int isExist = orgMapper.findSame(deptName, oldOrg.getPid());
        if (isExist > 0) {
            map.put("msg", "该部门名已存在!");
            return map;
        }
        org.setId(id);
        org.setDeptname(deptName);
        int isSuccess = orgMapper.alterName(org);
        if (isSuccess > 0) {
            map.put("msg", "修改成功!");
        } else {
            map.put("msg", "修改失败!");
        }
        return map;
    }

    /**
     * 新建节点
     *
     * @param pid      新建节点的父亲id
     * @param deptName 新建节点的名称
     * @author houxy
     */
    @Override
    public Map<String, Object> newNode(String pid, String deptName) {
        Map<String, Object> map = new HashMap<String, Object>();
        Organization org = new Organization();
        int isExist = orgMapper.findSame(deptName, pid);
        if (isExist > 0) {
            map.put("msg", "该部门名已存在!");
            return map;
        }
        org.setPid(pid);
        org.setDeptname(deptName);
        int isSuccess = orgMapper.newNode(org);
        if (isSuccess > 0) {
            map.put("msg", "节点添加成功!");
        } else {
            map.put("msg", "节点添加失败!");
        }
        return map;
    }

    /**
     * 删除选中节点及其子节点
     *
     * @param id 删除节点的id
     * @author houxy
     */
    @Override
    public Map<String, Object> deleteNode(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        orgMapper.deleteNodes(id);
        map.put("msg", "删除成功!");
        return map;
    }

    /**
     * 移动节点
     *
     * @param sid 想要转移的节点的id
     * @param pid 转移到节点的id
     * @author houxy
     */
    @Override
    public Map<String, Object> moveNode(String sid, String pid) {
        Map<String, Object> map = new HashMap<String, Object>();
        //根据id查重
        Organization sameOrg = orgMapper.findSameByOrgId(sid, pid);
        if (sameOrg != null && !sameOrg.getId().equals(sid)) {
            map.put("msg", "该节点下已有相同名字的子节点，请重新选择!");
            return map;
        }
        Organization org = new Organization();
        org.setId(sid);
        org.setPid(pid);
        orgMapper.moveNode(org);
        map.put("msg", "转移成功!");
        return map;
    }

    /**
     * 根据部门id获取部门名称
     *
     * @param orgId
     * @return
     */
    @Override
    public Organization findByOrgId(String orgId) {
        return orgMapper.findByOrgId(orgId);
    }

}
